layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        form = layui.form,
        table = layui.table,
        laydate = layui.laydate;

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'keyWord/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '关键词列表',
        height: globalHeight,
        page: true,
        cols: [[
            {field: 'id', title: 'ID', width: 110, align: 'center'},
            {field: 'word', title: '关键词', width: 330, align: 'center'},
            {field: 'status', title: '状态', width: 210, align: 'center', templet: '#statusTpl'},
            {field: 'createTime', title: '提交时间', width: 240, align: 'center'},
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.keyWordLibs //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'keyWord/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
            case 'add':
                layer.open({
                    type: 2,
                    title: '添加关键词',
                    offset: '25%',
                    content: 'toAddKeyWord',
                    area: ['400px', '200px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {     //确定按钮回调
                        var iframeWindow = window['layui-layer-iframe'+ index],
                            submit = layero.find('iframe').contents().find("#data-submit");

                        //监听提交
                        iframeWindow.layui.form.on('submit(data-submit)', function(data){
                            var field = data.field; //获取提交的字段
                            // 加载层
                            var load = layerLoad();

                            //提交 Ajax 成功后，静态更新表格中的数据
                            post('keyWord/add', field, field, function (data) {
                                layer.msg("添加成功");
                                table.reload('data-table');
                                layer.close(index); //关闭弹层
                                layer.close(load); //关闭弹层
                            });
                        });

                        submit.trigger('click');
                    },
                });
                break;
        };
    });

    //监听表格元素操作
    form.on('switch(updateStatus)', function(obj){
        var id = this.value;
        layer.confirm('确定更改该关键词状态', {
            offset: '30%',
            btn1: function(index){
                // 加载层
                var load = layerLoad();
                post('keyWord/updateStatus', {'id': id}, false, function (data) {
                    layer.msg("状态更改成功");
                    table.reload('data-table');
                    layer.close(index);
                    layer.close(load);
                }, function () {
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render();
                });
            },
            btn2: function(index){
                $(obj.elem).prop('checked', !obj.elem.checked);
                form.render();
            },
            cancel: function(index){
                $(obj.elem).prop('checked', !obj.elem.checked);
                form.render();
            }
        });
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var word = $('#word').val();
        var status = $('#status').val();
        var time = $('#time').val();

        return {'word': word, 'status': status, 'createTime': time};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#word').val(data.word);
        $('#status').val(data.status);
        $('#time').val(data.createTime);
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});